#!/usr/bin/env python
#
#       Events.py
#       
#       Copyright 2009 Ben Davis <its.hip@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import pygame.event

pygame.event.set_allowed(None)
pygame.event.set_allowed((pygame.MOUSEBUTTONDOWN, pygame.MOUSEMOTION))

class Events(object):
    def __init__(self):
        # By default, don't listen for any events until they're bound
        self.bindings = []
        
    def bind(self, function, *args, **dictionary):
        # Add our function with criteria and args to call with to the 'bindings' list
        self.bindings.append((function, args, dictionary))
        try:
            pygame.event.set_allowed(dictionary['type'])
        except KeyError:
            # Didn't specify type=something
            pass
            
    def unbind(self, **criteria):
        # For each binding
        for function, args, dictionary in self.bindings:
            # For each criterion
            for key in criteria.keys():
                try:
                    # If the criterion isn't satisfied by the current binding
                    if criteria[key] != dictionary[key]:
                        # Stop looping through this binding's criteria
                        break
                except KeyError:
                    break
            # If all criteria was satisfied for this binding
            else:
                # Remove the binding
                self.bindings.remove((function, args, dictionary))
        
    def parse(self, event):
        # For each binding
        for function, args, dictionary in self.bindings:
            # For each criterion in this binding
            for key in dictionary.keys():
                try:
                    if key == 'type':
                        if event.type != dictionary[key]:
                            break
                    else:
                        # If the criterion isn't satisfied by the current event
                        if event.dict[key] != dictionary[key]:
                            # Stop looping through this binding's criteria
                            break
                except KeyError:
                    break
            # If all criteria was satisfied for this binding
            else:
                # Call the function
                # If function returns False, event might be parsed again
                return function(event, *args)
        # Return False if event didn't match any bindings
        return False
